<?php
/*
 * @Date: 2020-05-03 22:03:52
 * @名称: 站点控制器
 * @版本: 0.01
 * @作者: 初雪桜
 * @邮箱: 202184199@qq.com
 * @最后编辑人: 初雪桜
 * @LastEditTime: 2021-03-17 23:59:17
 * @FilePath: /Site/app/useradmin/controller/SiteSetting.php
 */

namespace app\useradmin\controller;

use app\BaseController;
use PHPMailer\PHPMailer\PHPMailer;
use think\facade\Config;
use think\facade\View;

class SiteSetting extends BaseController
{
    public function Home()
    {
        return View::fetch("SiteSetting/SiteSetting", [
            "useradmin" => config("useradminConfig.useradmin_serverHost"),
        ]);
    }
    public function ApiSetting() //api 设置页面
    {
        // $config = \think\Config::load(APP_PATH . 'api_v1/config.php');
        $config = Config::load(app_path() . "../api/config/apiconfig.php", "aaa");
        return View::fetch("SiteSetting/Site/ApiSetting", [
            "useradmin" => config("useradminConfig.useradmin_serverHost"),
            "pageCount" => $config["comicsComicsInfoSelectCount"], //每页显示
            "chapterCount" => $config["comicsAllChapterSelectCount"], //章节数量
            "classCount" => $config["comicsAllClassSelectCount"], //分类数量
            "comicsCount" => $config["searchSelectCount"], //漫画数量
            "commentCount" => $config["commentCommentListSelectCount"], //评论数量
            "sonCommentCount" => $config["commentSonCommentListSelectCount"], //子评论数
        ]);
    }
    public function ApiExtendsSetting() //api 扩展设置
    {
        $config = Config::load(app_path() . "../api/config/apiconfig.php", "aaa");

        return View::fetch("SiteSetting/Site/ApiExtendsSetting", [
            "useradmin" => config("useradminConfig.useradmin_serverHost"),
            "TokenResolutionKey" => $config['TokenResolutionKey'],
            "TokenResolutionKeySha512" => $config['TokenResolutionKeySha512'],
            "UrlResolutionKeySha512" => $config['RequestToVerifyEncryptionKey'],
            "OpenOrNotRequestTimeOut" => $config['OpenOrNotRequestTimeOut'],
            "RequestTimeOut" => $config['RequestTimeOut'],
        ]);
    }
    public function SaveApiBasicSetting() //保存api基本设置
    {
        $onePageCount = input('post.onePageCount'); //一页显示多少
        $chapterCount = input('post.chapterCount'); //章节数量
        $classCount = input('post.classCount'); //分类数量
        $comicsCount = input('post.comicsCount'); //漫画数量
        $commentCount = input('post.commentCount'); //评论数量
        $sonCommentCount = input('post.sonCommentCount'); //子评论数
        $config = Config::load(app_path() . "../api/config/apiconfig.php", "aaa");
        if ( // 判断必要参数为空 并且数据类型不正确
            $onePageCount == null ||
            $chapterCount == null ||
            $classCount == null ||
            $comicsCount == null ||
            $commentCount == null ||
            $sonCommentCount == null ||
            !is_numeric($onePageCount) ||
            !is_numeric($chapterCount) ||
            !is_numeric($classCount) ||
            !is_numeric($comicsCount) ||
            !is_numeric($commentCount) ||
            !is_numeric($sonCommentCount)
        ) {
            return json_decode(errorJsonReturn("Admin-ApiSetting-SaveApiBasicSetting-10000")->getContent(), true);
        } else {
            $onePageCount = (int)ceil($onePageCount);
            $chapterCount = (int)ceil($chapterCount);
            $classCount = (int)ceil($classCount);
            $comicsCount = (int)ceil($comicsCount);
            $commentCount = (int)ceil($commentCount);
            $sonCommentCount = (int)ceil($sonCommentCount);
        }

        $setState = setconfig(app_path() . "../api/config/apiconfig.php", [
            "comicsComicsInfoSelectCount",
            "comicsAllChapterSelectCount",
            "comicsAllClassSelectCount",
            "searchSelectCount",
            "commentCommentListSelectCount",
            "commentSonCommentListSelectCount"
        ], [
            $onePageCount,
            $chapterCount,
            $classCount,
            $comicsCount,
            $commentCount,
            $sonCommentCount
        ]);
        if ($setState) {
            return json_decode(successJsonReturn()->getContent(), true);
        } else {
            return json_decode(errorJsonReturn("Admin-ApiSetting-SaveApiBasicSetting-10002")->getContent(), true);
        }
    }
    public function SaveApiExtendsSetting() //保存api扩展设置
    {
        $tokenParserAuthcode = input('post.tokenParserAuthcode');
        $tokenParserSha512 = input('post.tokenParserSha512');
        $urlVerificationSha512 = input('post.urlVerificationSha512');
        $ifTimeOutInput = input('post.ifTimeOutInput');
        $timeOutValue = input('post.timeOutValue');
        if (
            $tokenParserAuthcode == null ||
            $tokenParserSha512 == null ||
            $urlVerificationSha512 == null
        ) {
            return json_decode(errorJsonReturn("Admin-ApiSetting-ApiExtendsSetting-10000")->getContent(), true);
        }
        if ((intval($ifTimeOutInput) == null) || ($timeOutValue == false)) { //判断请求时间验证
            $ifTimeOutInput = intval($ifTimeOutInput);
            $timeOutValue = false;
        } else { //判断请求时间验证
            $ifTimeOutInput = intval($ifTimeOutInput);
            $timeOutValue = true;
        }

        $setState = setconfig(app_path() . "../api/config/apiconfig.php", [
            "TokenResolutionKey",
            "TokenResolutionKeySha512",
            "RequestToVerifyEncryptionKey",
            "OpenOrNotRequestTimeOut",
            "RequestTimeOut",
        ], [
            $tokenParserAuthcode,
            $tokenParserSha512,
            $urlVerificationSha512,
            $ifTimeOutInput,
            $timeOutValue,
        ]);
        if ($setState) {
            return json_decode(successJsonReturn()->getContent(), true);
        } else {
            return json_decode(errorJsonReturn("Admin-ApiSetting-SaveApiBasicSetting-10002")->getContent(), true);
        }
    }
    public function SystemSetting()
    {
        return View::fetch("SiteSetting/Site/SystemSetting", [
            "useradmin" => config("useradminConfig.useradmin_serverHost"),
            "smtpSeverHost" => config("smtp.smtpSeverHost"),
            "smtpSeverBodyChar" => config("smtp.smtpSeverBodyChar"),
            "smtpSeverUser" => config("smtp.smtpSeverUser"),
            "smtpSeverPass" => config("smtp.smtpSeverPass"),
            "smtpSeverMail" => config("smtp.smtpSeverMail"),
            "smtpSeverPort" => config("smtp.smtpSeverPort"),
            "smtpSeverName" => config("smtp.smtpSeverName"),
            "smtpReplyAddress" => config("smtp.smtpReplyAddress"),
        ]);
    }

    public function SmtpSettingTest()
    {
        $smtpSeverBodyChar = input("post.smtpSeverBodyChar");
        $smtpSeverPort = input("post.smtpSeverPort");
        $smtpSeverUser = input("post.smtpSeverUser");
        $smtpSeverPass = input("post.smtpSeverPass");
        $smtpSeverMail = input("post.smtpSeverMail");
        $smtpSeverName = input("post.smtpSeverName");
        $smtpReplyAddress = input("post.smtpReplyAddress");
        $testMailAdds = input("post.testMailAdds");

        try {
            $mail = new PHPMailer();
            //邮件调试模式
            $mail->SMTPDebug = 0;
            //设置邮件使用SMTP
            $mail->isSMTP();
            // 设置邮件程序以使用SMTP
            $mail->Host = config("smtp.smtpSeverHost");
            // 设置邮件内容的编码
            $mail->CharSet = $smtpSeverBodyChar;
            // 启用SMTP验证
            $mail->SMTPAuth = true;
            // SMTP username
            $mail->Username = $smtpSeverUser;
            // SMTP password
            $mail->Password = $smtpSeverPass;
            // 启用TLS加密，`ssl`也被接受
            $mail->SMTPSecure = 'tls';
            // 连接的TCP端口
            $mail->Port =  $smtpSeverPort;
            //设置发件人
            $mail->setFrom($smtpSeverMail, $smtpSeverName);
            //  添加收件人1
            $mail->addAddress($testMailAdds);     // Add a recipient
            //            $mail->addAddress('ellen@example.com');               // Name is optional
            //            收件人回复的邮箱
            $mail->addReplyTo($smtpReplyAddress, $smtpSeverName);
            //            抄送
            //            $mail->addCC('cc@example.com');
            //            $mail->addBCC('bcc@example.com');
            // 将电子邮件格式设置为HTML
            $mail->isHTML(true);
            $mail->Subject = "测试邮件";
            $mail->Body    = "测试邮件";
            //            $mail->AltBody = '这是非HTML邮件客户端的纯文本';
            $mail->send();
            if ($mail->ErrorInfo == "") {
                return json_decode(successJsonReturn()->getContent(), true);
            } else {
                return json_decode(errorJsonReturn($mail->ErrorInfo)->getContent(), true);
            }
        } catch (\Throwable $th) {
            return json_decode(errorJsonReturn($th)->getContent(), true);
        }
    }

    public function SmtpSettingSave()
    {
        $smtpSeverBodyChar = input("post.smtpSeverBodyChar");
        $smtpSeverPort = input("post.smtpSeverPort");
        $smtpSeverUser = input("post.smtpSeverUser");
        $smtpSeverPass = input("post.smtpSeverPass");
        $smtpSeverMail = input("post.smtpSeverMail");
        $smtpSeverName = input("post.smtpSeverName");
        $smtpReplyAddress = input("post.smtpReplyAddress");

        $setState = setconfig(app_path() . "../../config/smtp.php", [
            "smtpSeverBodyChar",
            "smtpSeverPort",
            "smtpSeverUser",
            "smtpSeverPass",
            "smtpSeverMail",
            "smtpSeverName",
            "smtpReplyAddress",
        ], [
            $smtpSeverBodyChar,
            $smtpSeverPort,
            $smtpSeverUser,
            $smtpSeverPass,
            $smtpSeverMail,
            $smtpSeverName,
            $smtpReplyAddress,
        ]);
        if ($setState) {
            return json_decode(successJsonReturn()->getContent(), true);
        } else {
            return json_decode(errorJsonReturn("Admin-ApiSetting-SaveApiBasicSetting-10002")->getContent(), true);
        }
    }
}
